package com.example.logindemoforjava.slice;

import com.example.logindemoforjava.MainAbility2;
import com.example.logindemoforjava.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;


public class LoginAbilitySlice extends AbilitySlice {
    private Button btnLogin;
    private TextField tfUsername, tfPassword;
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, "MainAbilitySlice.class");
    public static final int REQUEST_CODE_MAINABILITY=1;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_login);
        btnLogin = findComponentById(ResourceTable.Id_btnLogin);
        tfUsername = findComponentById(ResourceTable.Id_username);
        btnLogin.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.info(LABEL_LOG, "click");
                new ToastDialog(LoginAbilitySlice.this).setText("点击了登录按钮").setDuration(2000).show();
                //present(new MainAbilitySlice(),new Intent()); 无传参跳转
                //带参数跳转
                Intent intent = new Intent();
                intent.setParam("username", "陈奕铿");
                //present(new MainAbilitySlice(), intent);
                //presentForResult(new MainAbilitySlice(),intent,REQUEST_CODE_MAINABILITY);
                intent.setOperation(new Intent.OperationBuilder().withAbilityName(MainAbility2.class).withBundleName("com.example.myapplication3.hmservice").build());
                startAbility(intent);
            }
        });
    }

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode,resultIntent);
        if(requestCode==REQUEST_CODE_MAINABILITY){
            new ToastDialog(this).setText(resultIntent.getStringParam("result")).setDuration(2000).show();
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
